#!/usr/bin/env python3

#-------------------------------------------------------------------------------
# Name:        pic - Python IRC Client
# Purpose:     a simple Python IRC Client
#
# Author:      Christopher Koch
#
# Created:     29/08/2013
# Copyright:   (c) Christopher 2013
# Licence:     GPL 3
#-------------------------------------------------------------------------------

import termutils
import argparse
import configparser


## Global defaults ##


pic_version = (0, 0, 1)
version_string = "pic - Python IRC Client v. {}".format('.'.join(pic_version))

irc_server = 'chat.freenode.net'
irc_port = 6667
irc_channels = '#freenode'
use_ssl = False
irc_nick = None  # ALWAYS required, whether by command-line arg or config file
nick_pass = ''
serv_pass = ''
chan_pass = ''

nick_prefixes = True
highlight_color = 'yellow'
win_timestamp = '[%H:%M]'
log_timestamp = '[%H:%M:%S]'
fmt_chan_msg = '{0} <{1}>: {6}'
fmt_notice = '{0} {1} ({4}) NOTICE: {6}'
fmt_join = '{0} == <{1}> [{2}@{3}] has joined {4}'
fmt_part = '{0} == <{1}> [{2}@{3}] has left {4} ({5})'
fmt_quit = '{0} == <{1}> [{2}@{3}] has quit ({5})'
fmt_action = '{0} * {1} {6}'

header_bg = 'blue'
header_fg = 'cyan'
chat_bg = 'black'
chat_fg = 'white'
winlist_bg = 'blue'
winlist_fg = 'cyan'
winlist_cur_bg = 'red'
winlist_cur_fg = 'white'
input_bg = 'black'
input_fg = 'white'

irc_logging = False
irc_log_path = '~/.irc/'
irc_log_fmt = '{0}_{1}.log'  # freenode_freenode.log
debug_logging = True
debug_log_path = '/var/log/irc'
config_paths = ['./pic-config', '~/.pic/pic-config', '~/.irc/pic-config', '/etc/pic-config', '/etc/pic/pic-config', '/etc/irc/pic-config']


## Window classes ##


class IRCWindow:
    def __init__(self, server, target):
        pass
        
    def show(self):
        ## Switch to this window ##
        pass
        
    def say(self, msg):
        ## Add msg to buffer and refresh window ##
        # this doesn't have anything to do with
        # sending or receiving IRC commands, only
        # with displaying them on the screen
        pass
        
    def update_head(self, new_head):
        ## Update the header ##
        pass
        
    def scroll_up(self):
        ## Scroll up one line ##
        pass
        
    def scroll_down(self):
        ## Scroll down one line ##
        pass
        
    def page_up(self):
        ## Scroll up one page ##
        pass
        
    def page_down(self):
        ## Scroll down one page ##
        pass
        
class WindowList:
    ## The window list is two dictionaries listing
    ## windows by event target and event source.
    ## Our subclass of ircutils.client will listen
    ## for messages and direct them to the appropriate
    ## window using the dictionaries.
    def __init__(self):
        pass
        
    def switch_to(self, win):
        ## Switch to a window by index ##
        pass
        
    def switch_left(self):
        ## Switch to the previous window ##
        pass
        
    def switch_right(self):
        ## Switch to the next window ##
        pass
        
    def del_window(self, win):
        ## Part channel if necessary and close window by index ##
        pass
        
    def add_window(self, source, target):
        ## Add a new window ##
        pass


## Utility functions ##


def list_from_str(s, delim=','):
	# needed for getting a list from argparse
	return s.split(delim)
    
    
def main():
    argparser = argparse.ArgumentParser(prog='pic', description="A simple command-line IRC client written in Python.")
    argparser.add_argument('-v', '--version', action='version', version=version_string)
    argparser.add_argument('-s', '--server', help="server IP or hostname to connect to")
    argparser.add_argument('-p', '--port', help="port to connect to")
    argparser.add_argument('-c', '--channels', help="list of channels to join", nargs='+')
    argparser.add_argument('--ssl', help="use SSL", action='store_true')
    argparser.add_argument('--connect-strings', help="list of <server>:<port>/<channel> strings to connect to, " \
                           "useful if you want to connect to multiple servers", nargs='+')
    argparser.add_argument('-n', '--nick', help="nickname to use")
    argparser.add_argument('-i', '--identify', help="password for NickServ")
    argparser.add_argument('--config-file', help="an alternate config file to use")
    argparser.add_argument('--irc-logging', help="enable chat logging and store logs in provided directory")
    argparser.add_argument('--no-debug-log', help="disable debug logging", action='store_false')
    # parse args, then check for config_file
    # attempt to read config file, params in config file override defaults
    # parse remaining args, params in args override config file params


if __name__ == '__main__':
    main()